import pygame


class Ship():

    def __init__(self, ai_settings, screen):
        '''初始化飞船'''
        self.screen = screen
        self.ai_settings = ai_settings

        # 加载飞船外形并获取外部矩形范围
        self.image = pygame.image.load('images/ship.png')
        self.rect = self.image.get_rect()
        self.screen_rect = screen.get_rect()

        # 将飞船放在屏幕底部中央
        self.rect.centerx = self.screen_rect.centerx
        self.rect.bottom = self.screen_rect.bottom

        # 飞船移动标志
        self.move_right = False
        self.move_left = False

        # 存储小数速度值
        self.center = float(self.rect.centerx)

    def update(self):
        # 根据标志移动位置
        if self.move_right and self.rect.right < self.screen_rect.right:
            self.center += self.ai_settings.ship_speed
        if self.move_left and self.rect.left > 0:
            self.center -= self.ai_settings.ship_speed

        self.rect.centerx = self.center

    def blitme(self):
        '''指定位置放置飞船'''
        self.screen.blit(self.image, self.rect)
